//
//  GQHRoutesController.m
//  Xanthium
//
//  Created by GuanQinghao on 08/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHRouteModel.h"

#pragma mark Object

#pragma mark View
#import "GQHRoutesView.h"

#pragma mark Controller
#import "GQHRoutesController.h"
#import "GQHRouteFreightsController.h"
#import "GQHRouteTrucksController.h"
#import "GQHRouteNewController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHRoutesController () <GQHRoutesViewDelegate> {
    
#pragma mark Data
    /// 参数
    NSMutableDictionary *parameters;
    /// 路线模型
    __block NSMutableArray *routeModelArray;
#pragma mark View
    /// 自定义根视图
    GQHRoutesView *rootView;
}

@end


@implementation GQHRoutesController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHRoutesView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"token"];
    
    [self fetchServiceData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"我的路线";
    [self.qh_rightButton setTitle:@"新增" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [rootView.qh_tableView.mj_header endRefreshing];
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        // 司机
        GQHRouteFreightsController *routeFreightsController = [[GQHRouteFreightsController alloc] init];
        routeFreightsController.qh_routeModel = routeModelArray[indexPath.row];
        routeFreightsController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:routeFreightsController animated:YES];
    } else {
        
        // 其他
        GQHRouteTrucksController *routeTrucksController = [[GQHRouteTrucksController alloc] init];
        routeTrucksController.qh_routeModel = routeModelArray[indexPath.row];
        routeTrucksController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:routeTrucksController animated:YES];
    }
}

- (void)qh_removeRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    GQHRouteModel *routeModel = routeModelArray[indexPath.row];
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buFocusOnLine/1/delete"];
    NSDictionary *parameters = @{@"id" : routeModel.qh_routeID};
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            
            [routeModelArray removeObjectAtIndex:indexPath.row];
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
    }];
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    GQHRouteNewController *routeNewController = [[GQHRouteNewController alloc] init];
    routeNewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:routeNewController animated:YES];
}

#pragma mark --PrivateMethod
- (void)fetchServiceData {
    
    __block int page;
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            /// 路线货源
            [GQHRouteModel qh_fetchRoutesFreightsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHRouteModel *> *routes) {
                
                if (status) {
                    
                    routeModelArray = [NSMutableArray arrayWithArray:routes];
                    rootView.qh_routeModelArray = routeModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_header endRefreshing];
                } else {
                    
                    [self showToastWithText:message];
                    [rootView.qh_tableView.mj_header endRefreshing];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
                [rootView.qh_tableView.mj_header endRefreshing];
            }];
        } else {
            
            /// 路线空车
            [GQHRouteModel qh_fetchRoutesTrucksWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHRouteModel *> *routes) {
                
                if (status) {
                    
                    routeModelArray = [NSMutableArray arrayWithArray:routes];
                    rootView.qh_routeModelArray = routeModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_header endRefreshing];
                } else {
                    
                    [self showToastWithText:message];
                    [rootView.qh_tableView.mj_header endRefreshing];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
                [rootView.qh_tableView.mj_header endRefreshing];
            }];
        }
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [GQHRouteModel qh_fetchRoutesFreightsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHRouteModel *> *routes) {
                
                if (status) {
                    
                    if (routes && routes.count > 0) {
                        
                        routeModelArray = [NSMutableArray arrayWithArray:routes];
                        rootView.qh_routeModelArray = routeModelArray;
                        [rootView.qh_tableView reloadData];
                        [rootView.qh_tableView.mj_footer endRefreshing];
                    } else {
                        
                        [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                } else {
                    
                    [self showToastWithText:message];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }];
        } else {
            
            [GQHRouteModel qh_fetchRoutesTrucksWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHRouteModel *> *routes) {
                
                if (status) {
                    
                    if (routes && routes.count > 0) {
                        
                        routeModelArray = [NSMutableArray arrayWithArray:routes];
                        rootView.qh_routeModelArray = routeModelArray;
                        [rootView.qh_tableView reloadData];
                        [rootView.qh_tableView.mj_footer endRefreshing];
                    } else {
                        
                        [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                } else {
                    
                    [self showToastWithText:message];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }];
        }
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
